#include "EC801.h"

extern UART_HandleTypeDef hlpuart1;
extern Value_64 V64;
extern Value_32 V32;
extern Value_Flag VFlag;

void EC801_Init(uint8_t Flag_EC801)
{
    char at_cmd[50];
	switch(Flag_EC801)
	{
		case 0:
			sprintf(at_cmd, "AT\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 50);
			Delay_ms(300);
			break;
		case 1:
//			sprintf(at_cmd, "AT+GMR\r\n");
			sprintf(at_cmd, "AT+CIMI\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 50);
			Delay_ms(300);
			break;
		case 2:
			sprintf(at_cmd, "AT+CSQ\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 50);
			Delay_ms(300);
			break;
		case 3:
			sprintf(at_cmd, "AT+CGATT=1\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 50);
			Delay_ms(300);
			break;
		case 4:
			sprintf(at_cmd, "AT+QLTS=2\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 50);
			Delay_ms(300);
			break;
		case 5:
			V32.Data = VFlag.IP_Addr1;
//			sprintf(at_cmd, "AT+QIOPEN=1,0,TCP,%d.%d.%d.%d,%d\r\n",\
//			V32.Buf[3],V32.Buf[2],V32.Buf[1],V32.Buf[0],VFlag.TCP_Port1);
			sprintf(at_cmd, "AT+QIOPEN=1,0,TCP,7dy8083cw385.vicp.fun,47238\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 100);
			Delay_ms(300);
			break;
		
		case 6:
			sprintf(at_cmd, "AT+QICLOSE=0\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 500);
			Delay_ms(300);
			break;
		
		case 7:
			sprintf(at_cmd, "AT+QIRD=0,255\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 500);
			Delay_ms(300);
			break;

	}
}

void Send_Data_To_Server(uint8_t *data)
{
    char at_cmd[100];
	
	uint8_t len;
	len = data[6] + 10;
	if(len > 255)len = 255;
	if(data[5] == Fun_Requst)len = 9;
	if(data[5] == Fun_Affirm)len = 9;
	if(data[5] == Fun_End)len = 9;
	
    sprintf(at_cmd, "AT+QISEND=0,%d\r\n",len);
    HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 100);
	Delay_ms(300);

    HAL_UART_Transmit(&hlpuart1, (uint8_t *)data, len, 100);
	Delay_ms(300);

//	if(VFalg.Gather_Cnt > 0)
//	{
    uint8_t ctrl_z = 0x1A;
    HAL_UART_Transmit(&hlpuart1, &ctrl_z, 1, 100);
	Delay_ms(300);
//	if(strstr((const char*)Recv_Buf,(const char*)"QISEND=0"))memset(Recv_Buf, 0, sizeof(Recv_Buf));

}



//AT+QHTTPCFG="contextid",1
//AT+QHTTPCFG="responseheader",0
//AT+QIACT?
//AT+QICSGP=1,1,"CMNET","","",1
//AT+QIACT=1
//AT+QIACT?
//AT+QHTTPURL=64,80
//https://www.lzyb.net/storage/upgrade/Desktop.bin
//AT+QHTTPGET=80
//AT+QHTTPREADFILE="app.bin",80
void OTA_Data_Read(uint8_t Flag_OTA)
{
    char at_cmd[100];
	switch(Flag_OTA)
	{
		case 0:
			sprintf(at_cmd, "AT+QHTTPCFG=contextid,1\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 50);
			Delay_ms(300);
			break;
		
		case 1:
			sprintf(at_cmd, "AT+QHTTPCFG=requestheader,0\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 50);
			Delay_ms(300);
			break;
		
		case 2:
			sprintf(at_cmd, "AT+QIACT?\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 50);
			Delay_ms(300);
			
			break;
		
		case 3:
			sprintf(at_cmd, "AT+QHTTPURL=49,80\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 500);
			Delay_ms(300);
			break;
		
		case 4:
//			sprintf(at_cmd, "https://www.lzyb.net/storage/upgrade/app_crc.bin\r\n");
			sprintf(at_cmd, "http://www.ssyllj.com/storage/upgrade/app_crc.bin\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 500);
			Delay_ms(300);
			break;
		
		case 5:
			sprintf(at_cmd, "AT+QHTTPGET=80\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 500);
			Delay_ms(1000);
			break;
		
		case 6:
			Flash_OTAUP_Erase();
			Delay_ms(3000);
			sprintf(at_cmd, "AT+QHTTPREAD=80\r\n");
			HAL_UART_Transmit(&hlpuart1, (uint8_t *)at_cmd, strlen(at_cmd), 500);
			break;

		default:
			
			break;

	}
}





